package org.dromara.business.controller;

import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.Goods;
import org.dromara.business.domain.vo.GoodsVo;
import org.dromara.business.domain.vo.ReportVo;
import org.dromara.business.domain.vo.SellerGoodsVo;
import org.dromara.business.service.GoodsService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品
 *
 * @author zlj
 * @date 2024-08-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/goods")
public class GoodsController extends BaseController {

    private final GoodsService goodsService;

    /**
     * 商品列表
     */
    @GetMapping("/list")
    public TableDataInfo<GoodsVo> list(Goods bo, PageQuery pageQuery) {
        return goodsService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取商品详细信息
     *
     * @param goodsId 主键
     */
    @GetMapping("/{goodsId}")
    public R<GoodsVo> getInfo(@NotNull(message = "主键不能为空")
                              @PathVariable Long goodsId) {
        return R.ok(goodsService.queryById(goodsId));
    }

    /**
     * 新增商品
     */
    @RepeatSubmit()
    @PostMapping("/add")
    public R<Long> add(@Validated(AddGroup.class) @RequestBody Goods bo) {
        return R.ok(goodsService.insertByBo(bo));
    }

    /**
     * 修改任务
     */
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody Goods bo) {
        return toAjax(goodsService.updateByBo(bo));
    }

    /**
     * 修改任务状态
     */
    @PutMapping("/updateGoodsStatus")
    public R<Void> updateGoodsStatus(Long goodsId, Integer status, String infoDesc) {
        return toAjax(goodsService.updateGoodsStatus(goodsId, status, infoDesc));
    }

    /**
     * 重新审核
     */
    @RepeatSubmit()
    @PutMapping("/goodsUp")
    public R<Void> goodsUp(Long goodsId) {
        return toAjax(goodsService.goodsUp(goodsId));
    }

    /**
     * 删除任务
     *
     * @param goodsIds 主键串
     */
    @DeleteMapping("/{goodsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] goodsIds) {
        return toAjax(goodsService.deleteWithValidByIds(List.of(goodsIds), true));
    }

    /**
     * 强制下架
     *
     * @param goodsId
     */
    @GetMapping("/forceShelve/{goodsId}")
    public R<ReportVo> approve(@NotNull(message = "主键不能为空")
                               @PathVariable Long goodsId) {
        Goods goods = goodsService.getById(goodsId);
        if (goods == null) {
            throw new RuntimeException("没有查到相关记录");
        }
        goodsService.updateById(goods);
        //todo 强制下架后 (暂定, 看后续客户是否需要追加)
        // 1.清空其他购物车用户任务
        // 2.消息主动推送给上架者,IM消息 + Message表数据
        // 3....................
        return R.ok();
    }


    /**
     * 我的-我的任务-卖家查看任务在售情况(交易中/已卖出)
     *
     * @param type: 1.交易中 2.已卖出
     */
    @GetMapping("/sellerListGoods")
    public R sellerListGoods(@RequestParam("type") int type) {
        List<SellerGoodsVo> sellerGoodsVos = goodsService.sellerListGoods(String.valueOf(LoginHelper.getUserId()), type);
        return R.ok(sellerGoodsVos);
    }

    /**
     * 上下架
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/up")
    public R<Void> up(@RequestParam Long goodsId) {
        return toAjax(goodsService.up(goodsId));
    }

    /**
     * 接单
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/takeOrders")
    public R<Void> takeOrders(@RequestParam Long goodsId) {
        return toAjax(goodsService.takeOrders(goodsId));
    }

    /**
     * 送达
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/served")
    public R<Void> served(@RequestParam Long goodsId) {
        return toAjax(goodsService.served(goodsId));
    }

    /**
     * 确认收货
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/receiving")
    public R<Void> receiving(@RequestParam Long goodsId) {
        return toAjax(goodsService.receiving(goodsId));
    }

    /**
     * 发布人取消订单
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/remove")
    public R<Void> removeGoods(@RequestParam Long goodsId) {
        return toAjax(goodsService.removeGoods(goodsId));
    }

    /**
     * 接单人取消订单
     *
     * @param goodsId 任务id
     */
    @RepeatSubmit(interval = 1000, message = "正在处理, 请稍后")
    @PutMapping("/removeByUser")
    public R<Void> removeByUser(@RequestParam Long goodsId) {
        return toAjax(goodsService.removeByUser(goodsId));
    }
}
